package com.andyliu.configer;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @packageName: com.andyliu.configer
 * @user: andyliu
 * @date: 2021/8/2 14:56
 * @email 115176513@qq.com
 * @description: TODO
 */
@Configuration
public class DataSourceConfiger {
    @Primary
    @Bean("db1DataSourceProperties")
    @ConfigurationProperties(prefix = "spring.db1")
    public DataSourceProperties getDataSourceProperties1(){
        return new DataSourceProperties();
    }

    @Bean("db2DataSourceProperties")
    @ConfigurationProperties(prefix = "spring.db2")
    public DataSourceProperties getDataSourceProperties2(){
        return new DataSourceProperties();
    }

    // 主数据源
    @Primary
    @Bean("db1DataSource")
    public DataSource getDataSource1(@Qualifier("db1DataSourceProperties") DataSourceProperties properties){
        return properties.initializeDataSourceBuilder().build();
    }
    // 第二个数据源
    @Bean("db2DataSource")
    public DataSource getDataSource2(@Qualifier("db2DataSourceProperties") DataSourceProperties properties){
        return properties.initializeDataSourceBuilder().build();
    }

}
